package cn.mesmile.order.controller;

import cn.mesmile.daomain.Video;
import cn.mesmile.daomain.VideoOrder;
import cn.mesmile.order.service.VideoService;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author zb
 * @version 1.0
 * @date 2020/10/24 13:56
 * @description
 */
@RestController
@RequestMapping("/api/v1/order")
public class OrderController {

    private final RestTemplate restTemplate;

    private final DiscoveryClient discoveryClient;

    private final VideoService videoService;

    public OrderController(RestTemplate restTemplate, DiscoveryClient discoveryClient, VideoService videoService) {
        this.restTemplate = restTemplate;
        this.discoveryClient = discoveryClient;
        this.videoService = videoService;
    }

    @GetMapping("/save")
    public Object save (@RequestParam("videoId") Integer videoId) {

//        List<ServiceInstance> instances =
//                discoveryClient.getInstances("xdclass-video-service");
//        ServiceInstance serviceInstance = instances.get(0);
//        String host = serviceInstance.getHost();
//        int port = serviceInstance.getPort();
//        String url = "http://"+host+":"+port+"/api/v1/video/getVideoById?videoId=";

        // ribbon 负载均衡调用
        String url = "http://xdclass-video-service/api/v1/video/getVideoById?videoId=";

        Video video = videoService.getVideoById(videoId);

//        Video video= restTemplate.getForObject(url + videoId, Video.class);
        Assert.notNull(video, "查询订单失败");
        VideoOrder videoOrder = new VideoOrder();
        videoOrder.setId(1);
        videoOrder.setVideoId(video.getId());
        videoOrder.setCreateTime(new Date());
        videoOrder.setVideoTitle(video.getTitle());
        videoOrder.setServerInfo(video.getServerInfo());
        return videoOrder;
    }


    @GetMapping("/list")
    public Object list() {
        try {
            // 睡 3秒
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // 测试多线程  限流    【 流控规则下发到微服务，微服务重启后，流控规则就会消息，可以持久化配置】
        Map<String, String> map = new HashMap<>();
        map.put("title1", "alibaba 微服务专题");
        map.put("title2", "spring 微服务专题");
        return map;
    }


    /**
     *  测试降级业务
     */
    int temp = 0;
    @GetMapping("/list2")
    public Object list2 (HttpServletRequest request) {
        temp ++;
        if (temp % 3 == 0) {
//            throw new RuntimeException();
        }
        String remoteHost = request.getRemoteHost();
        int serverPort = request.getServerPort();

        Map<String, String> map = new HashMap<>();
        map.put("title1", "alibaba 微服务专题");
        map.put("title2", "spring 微服务专题");
        map.put("title3", remoteHost);
        map.put("title4", serverPort+"");
        return map;
    }






}
